package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.DeviceListBean;
import com.splant.android.smart.ModelLevel.ParaDatasBean;
import com.splant.android.smart.ModelLevel.PlantListBean;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_PlantList_Adapter;
import com.splant.android.smart.UILevel.Community.fragments.Garden_WaterList_Adapter;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Garden_PlantList_Search_Activity extends Activity implements View.OnClickListener {

    private EditText edt_searchKey;
    private TextView tv_cancel;
    private String keyword="";
    private sPlantAsyncHttpClient sPlantClient;

    private LRecyclerView lrv_plantList;
    private LRecyclerViewAdapter plantListRecyclerViewAdapter;
    private Garden_PlantList_Adapter garden_plantList_adapter;
    private List<PlantListBean.Plant> plantList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.garden_plantlist_search_activity);

        tv_cancel= (TextView) findViewById(R.id.tv_cancel);
        edt_searchKey= (EditText) findViewById(R.id.edt_searchKey);

        lrv_plantList= (LRecyclerView) findViewById(R.id.lrv_plantList);

        init();

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        edt_searchKey.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    keyword=v.getText().toString();

                    if (keyword.equals("")==false)
                    getDatas();
                }
                return true;
            }
        });

        tv_cancel.setOnClickListener(this);
    }

    private void init()
    {
        garden_plantList_adapter = new Garden_PlantList_Adapter(this);
        plantListRecyclerViewAdapter = new LRecyclerViewAdapter(garden_plantList_adapter);

        lrv_plantList.setHasFixedSize(true);
        lrv_plantList.setLayoutManager(new GridLayoutManager(this, 3));
        lrv_plantList.setAdapter(plantListRecyclerViewAdapter);
        lrv_plantList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setVertical(R.dimen.gaiaa_devicelist_dh)
                .setColorResource(R.color.garden_color0)
                .build();
        lrv_plantList.addItemDecoration(divider);

        lrv_plantList.setPullRefreshEnabled(false);
        lrv_plantList.setLoadMoreEnabled(false);

        garden_plantList_adapter.setOnItemClickListener(new Garden_PlantList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                PlantListBean.Plant plant= garden_plantList_adapter.getDataList().get(pos);

                Intent intent=new Intent(Garden_PlantList_Search_Activity.this,Garden_Plant_Activity.class);

                intent.putExtra("plantId",plant.getPlantId());

                startActivity(intent);
            }
        });
    }

    private void getDatas()
    {
        RequestParams params = new RequestParams();
        params.put("sreachName", keyword);
        params.put("pageNo", -1);

        sPlantClient.get(sPlantAsyncHttpClient.GETPLANTLIST, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try
                {
                    if (response.getInt("succeed") == 1) {

                        Gson gson = new Gson();
                        plantList.clear();

                        PlantListBean plantListBean = gson.fromJson(response.toString(), PlantListBean.class);

                        if (plantListBean.getPlantList()!= null) {
                            plantList.addAll(plantListBean.getPlantList());
                        }

                        garden_plantList_adapter.setDataList(plantList);

                        plantListRecyclerViewAdapter.notifyDataSetChanged();

                        if (plantList.size()==0)
                        {
                            Toast.makeText(Garden_PlantList_Search_Activity.this,"没搜到相关植物！",Toast.LENGTH_SHORT).show();
                        }

                    }

                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_PlantList_Search_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

}
